import matplotlib.pyplot as plt
import numpy as np

# 准备数据
n = 5
x = np.logspace(-1, 0, n)

y11 = np.array([84.8545,	84.149,	81.3933	,77.91006667,   74.04762])
y12 = np.array([0.3631,	0.431233333,	0.621233333,	0.793966667,	1])

y21 = np.array([84.24406667,	82.51366667,	81.42076667,	82.60473333,79.07106])
y22 = np.array([0.5277,	0.619733333,	0.6839,	0.851866667,	1])

y31 = np.array([43.76996667,	41.76653333,	37.6217,	31.0399,25.6173])
y32 = np.array([0.591266667,	0.6483,	0.7499,	0.8677,	1])

y41 = np.array([99.7708,	99.6398	,99.18136667,	97.93716667,94.34186])
y42 = np.array([0.726,	0.7796,	0.883133333,	0.953966667	,1])


y51 = np.array([58.45213333,	56.30916667,	54.34123333	,47.73913333    ,39.91464])
y52 = np.array([0.702866667	,0.7723	,0.853166667	,0.941166667	,1])

# 创建图形和坐标轴对象
fig, axs = plt.subplots(1, 5, figsize=(20, 4))

# 绘制第一个图
axs[0].plot(x, y11, label='bottom->top', color='steelblue', linestyle='-')
axs[0].scatter(x, y11, color='steelblue', marker='D', s=30)
axs[0].set_xticks(x)
axs[0].set_xticklabels(['1', '1→3', '1→6', '1→9', '1→12'])
axs[0].tick_params(axis='x', colors='black')

# 添加第二个Y轴
ax2_0 = axs[0].twinx()
ax2_0.plot(x[0:], y12, label='top->bottom', color='chocolate', linestyle='-.')
ax2_0.scatter(x[0:], y12, color='chocolate', marker='o', s=30)
ax2_0.tick_params(axis='y', colors='chocolate')

axs[0].set_ylabel('Test accuracy(%)', fontsize=18, fontweight='normal')
axs[0].set_title('ISIC', fontsize=18, fontweight='normal')
axs[0].grid(True, which='both', color='lightgray', alpha=0.8)

# 绘制第二个图
axs[1].plot(x, y21, label='top->bottom', color='steelblue', linestyle='-')
axs[1].scatter(x, y21, color='steelblue', marker='D', s=30)
axs[1].set_xticks(x)
axs[1].set_xticklabels(['1', '1→3', '1→6', '1→9', '1→12'])
axs[1].tick_params(axis='x', colors='black')

# 添加第二个Y轴
ax2_1 = axs[1].twinx()
ax2_1.plot(x[0:], y22, label='bottom->top', color='chocolate', linestyle='-.')
ax2_1.scatter(x[0:], y22, color='chocolate', marker='o', s=30)
ax2_1.tick_params(axis='y', colors='chocolate')

axs[1].set_title('APTOS', fontsize=18, fontweight='normal')
axs[1].grid(True, which='both', color='lightgray', alpha=0.8)

# 绘制第三个图
axs[2].plot(x, y31, label='top->bottom', color='steelblue', linestyle='-')
axs[2].scatter(x, y31, color='steelblue', marker='D', s=30)
axs[2].set_xticks(x)
axs[2].set_xticklabels(['1', '1→3', '1→6', '1→9', '1→12'])
axs[2].tick_params(axis='x', colors='black')

# 添加第二个Y轴
ax2_2 = axs[2].twinx()
ax2_2.plot(x[0:], y32, label='bottom->top', color='chocolate', linestyle='-.')
ax2_2.scatter(x[0:], y32, color='chocolate', marker='o', s=30)
ax2_2.tick_params(axis='y', colors='chocolate')
# ax2_2.set_ylabel('Similarity', fontsize=18, fontweight='normal', color='black')


axs[2].set_title('Chest', fontsize=18, fontweight='normal')
axs[2].grid(True, which='both', color='lightgray', alpha=0.8)

# 绘制第四个图
axs[3].plot(x, y41, label='top->bottom', color='steelblue', linestyle='-')
axs[3].scatter(x, y41, color='steelblue', marker='D', s=30)
axs[3].set_xticks(x)
axs[3].set_xticklabels(['1', '1→3', '1→6', '1→9', '1→12'])
axs[3].tick_params(axis='x', colors='black')

# 添加第二个Y轴
ax2_3 = axs[3].twinx()
ax2_3.plot(x[0:], y42, label='bottom->top', color='chocolate', linestyle='-.')
ax2_3.scatter(x[0:], y42, color='chocolate', marker='o', s=30)
ax2_3.tick_params(axis='y', colors='chocolate')
# ax2_3.set_ylabel('Similarity', fontsize=18, fontweight='normal', color='black')


axs[3].set_title('Colon', fontsize=18, fontweight='normal')
axs[3].grid(True, which='both', color='lightgray', alpha=0.8)


# 绘制第五个图
axs[4].plot(x, y51, label='top->bottom', color='steelblue', linestyle='-')
axs[4].scatter(x, y51, color='steelblue', marker='D', s=30)
axs[4].set_xticks(x)
axs[4].set_xticklabels(['1', '1→3', '1→6', '1→9', '1→12'])
axs[4].tick_params(axis='x', colors='black')

# 添加第二个Y轴
ax2_4 = axs[4].twinx()
ax2_4.plot(x[0:], y52, label='bottom->top', color='chocolate', linestyle='-.')
ax2_4.scatter(x[0:], y52, color='chocolate', marker='o', s=30)
ax2_4.tick_params(axis='y', colors='chocolate')
ax2_4.set_ylabel('Similarity', fontsize=18, fontweight='normal', color='black')


axs[4].set_title('Endo', fontsize=18, fontweight='normal')
axs[4].grid(True, which='both', color='lightgray', alpha=0.8)

# 设置标签颜色
for i_axs in axs:
    xtick_labels = i_axs.get_xticklabels()
    for label in xtick_labels:
        label.set_color('black')

# 共用一个图例
legend_handles = []
legend_labels = ['Acc', 'Sim']

twins_axs = [ax2_0,ax2_1,ax2_2,ax2_3,ax2_4] 
for i in range(5):
    legend_handles.append((axs[i].get_lines()[0], axs[i].collections[0]))
    legend_handles.append((twins_axs[i].get_lines()[0], twins_axs[i].collections[0]))

fig.legend(handles=legend_handles, labels=legend_labels, fontsize=18, loc='upper right', ncol=4)

# 调整子图的间距
fig.tight_layout()

plt.subplots_adjust(top=0.75)

# 保存图形
plt.savefig('Trade_off/a_frozen_layers.png')
